// Declare a string variable to store user input from the Serial Monitor
String inputMessage = "";

void setup() {
  // Initialize the USB Serial interface for communication with the Serial Monitor
  Serial.begin(9600);

  // Wait for the Serial Monitor to be opened before proceeding
  while (!Serial);

  // Initialize Serial1 (UART) for communication between RP2040 boards
  // On Seeed XIAO RP2040, Serial1 uses:
  // TX = D6 (physical pin 6), RX = D7 (physical pin 7)
  Serial1.begin(9600);

  // Notify the user that this board is ready to send messages
  Serial.println("Rwanda ready.");
}

void loop() {
  // Check if there is input available from the Serial Monitor (USB serial)
  if (Serial.available()) {
    // Read the entire input line (until newline character)
    inputMessage = Serial.readStringUntil('\n');

    // Send a formatted message over UART (Serial1) to the other board
    Serial1.print("Rwanda says: ");
    Serial1.println(inputMessage);

    // Print the same message locally on this board’s Serial Monitor
    Serial.print("Sent: ");
    Serial.println(inputMessage);
  }

  // Check if a message has been received from the other board over UART (Serial1)
  if (Serial1.available()) {
    // Read the full received message (until newline character)
    String msg = Serial1.readStringUntil('\n');

    // Display the received message in this board’s Serial Monitor
    Serial.println(msg);
  }
}